package com.team.app.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.team.app.service.BrandService;
import com.team.app.service.CityService;
import com.team.app.service.ModelService;
import com.team.app.service.VehicleService;


/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@Autowired	
	private BrandService brandService;
	
	@Autowired	
	private ModelService modelService;
	
	@Autowired	
	private VehicleService vehicleService;
	
	@Autowired	
	private CityService cityService;

		
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		model.addAttribute("vehicleList",vehicleService.findTopCar());
		setInital(model);	
		return "home/result-search";
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	public String search(Model model,Long provinces,Long cityId, Date pickupDate, Date returnDate, Long transmissionType, String seat_number, Long brandId) {
		model.addAttribute("vehicleList",vehicleService.findCar(pickupDate, returnDate, cityId, seat_number, brandId, transmissionType));
		/*model.addAttribute("citys", city);*/
		setInital(model);
		/*System.out.print("Transmission la: " +  transmissionType);
		System.out.print("cityID: "+ cityId);
		System.out.print("dateGo: "+ transmissionType);*/
		if (transmissionType == null)
			System.out.print("No null");
		return "home/result-search";
	}
	
	@RequestMapping(value = "/book", method = RequestMethod.GET)
	public String book(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		setInital(model);	
		return "booking";
	}
	
	/*---------------------------------------------------------------------------------------------------------------*/
	protected void  setInital(Model model) {	
		
		model.addAttribute("brands",brandService.findAll());
		
		model.addAttribute("models",modelService.findAll());
		
		model.addAttribute("citys", cityService.findAll());
		
		model.addAttribute("pickup_time", CreateTime());
		
		model.addAttribute("return_time", CreateTime());
	}
	
	private List<String> CreateTime()
	{
		List<String> times = new ArrayList<String>();
		
		for(int i = 0; i <= 23; i++)
		{
			if(i <= 9)
			{
				times.add("0"+ i + " : " + "00");
				times.add("0"+ i + " : " + "30");
			}
			else
			{
				times.add(i + " : " + "00");
				times.add(i + " : " + "30");
			}
		}
		
		return times;
	}
}
